package com.dly.blog.service.impl;

import cn.hutool.json.JSONUtil;
import com.dly.blog.domain.dto.GptRequestDto;
import com.dly.blog.domain.dto.MessageContentDto;
import com.dly.blog.domain.enums.ChatType;
import com.dly.blog.service.ChatClient;
import com.dly.blog.service.ChatProxy;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @Author: DaiLiYu
 * @Date: 2024/1/11 20:45
 */
@Service
@AllArgsConstructor
public class FourChatProxy implements ChatProxy {

    private final ChatClient client;

    @Override
    public boolean matching(ChatType chatType) {
        return chatType.equals(ChatType.FOUR_CHAT);
    }

    @Override
    public Flux<String> send(List<MessageContentDto> messages) {
        return client.getClient()
                .build()
                .post()
                .uri("/chat/completions")
                .bodyValue(JSONUtil.toJsonStr(
                        GptRequestDto.builder()
                                .model("gpt-4")
                                .stream(true)
                                .temperature(0.7)
                                .messages(messages)
                                .build()))
                .retrieve()
                .bodyToFlux(String.class);
    }
}
